package it.gat.webservice.resource.impl;

import java.sql.SQLException;

import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;

import it.gat.dao.TicketDAO;
import it.gat.model.Ticket;
import it.gat.util.dao.DAOFactory;
import it.gat.webservice.utils.Impostazioni;

public class TicketResource extends ServerResource{
	
	@Post
	public void insert(Ticket ticket) {
		DAOFactory daoFactory = DAOFactory.getDAOFactory(Integer.parseInt(Impostazioni.getProperty("DB_TYPE")));
		TicketDAO ticketDAO = daoFactory.getTicketDAO();
		try {
			ticketDAO.insert(ticket);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Get
	public Ticket retriveByCodice() {
		DAOFactory daoFactory = DAOFactory.getDAOFactory(Integer.parseInt(Impostazioni.getProperty("DB_TYPE")));
		TicketDAO ticketDAO = daoFactory.getTicketDAO();
		Ticket ticket;
		try {
			ticket = ticketDAO.retrieveById(Integer.parseInt(getRequestAttributes().get("id").toString()));
		} catch (NumberFormatException e) {
			return null;
		} catch (SQLException e) {
			return null;
		}
		return ticket;
	}

	@Put
	public void update(Ticket ticket) {
		DAOFactory daoFactory = DAOFactory.getDAOFactory(Integer.parseInt(Impostazioni.getProperty("DB_TYPE")));
		TicketDAO ticketDAO = daoFactory.getTicketDAO();
		try {
			ticketDAO.update(ticket);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Delete
	public void delete(Ticket ticket) {
		DAOFactory daoFactory = DAOFactory.getDAOFactory(Integer.parseInt(Impostazioni.getProperty("DB_TYPE")));
		TicketDAO ticketDAO = daoFactory.getTicketDAO();
		try {
			ticketDAO.delete(Integer.parseInt(getRequestAttributes().get("id").toString()));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
